//
//  LoggedViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-7.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "LoggedViewController.h"
#import "LoggedCell.h"
#import "MyOrderViewController.h"
#import "MyCollectViewController.h"
#import "MyCouponViewController.h"
#import "MyPhotoViewController.h"
#import "ActivityViewController.h"
#import "ReviewViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UCenterViewController.h"
#import "MeViewController.h"
#import "AppSetting.h"


#define kPERSONAL   1001
#define kMENU       1002
#define kLEFT       10001
#define kRIGHT      10002

//会员基本信息
#define kINDEX       2001
//我的点评
#define kREVIEW      2002

#define ShopHomeClass @"ShopHomeViewController"

@interface LoggedViewController ()

@end

@implementation LoggedViewController

- (id)init
{
    self = [super initWithNibClass:[LoggedViewController class]];
    if (self) {
        _dataSource = [[LoggedDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initData];
    [self setupUI];
    [_dataSource fetchUserInfo];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([AppSetting isUpdatePhoto]) {
        [_dataSource fetchUserInfo];
    }
    if (_backController) {
        _isHidden = [AppDelegate sharedInstance].tabBarController.tabbarView.hidden;
        [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
    }
}

- (void)setupUI
{
    [self createNavigation:@"个人会员中心"];
    [self createRightButtonWithTitle:@"" background:NAV_CLOSS_BACKGROUND];
    self.navigationItem.hidesBackButton = YES;
}

- (void)initData
{
    _menuList = [NSMutableArray arrayWithCapacity:10];
    _personalInfo = [NSMutableArray arrayWithCapacity:10];
    
    NSMutableDictionary *review = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *albums = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myOrder = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myCoupon = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *activity = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myCollect = [NSMutableDictionary dictionaryWithCapacity:10];
    
    [review setObject:@"点评" forKey:@"title"];
    [albums setObject:@"相册" forKey:@"title"];
    [activity setObject:@"活动" forKey:@"title"];
    [myOrder setObject:@"我的订单" forKey:@"title"];
    [myCollect setObject:@"我的收藏" forKey:@"title"];
    [myCoupon setObject:@"我的优惠券" forKey:@"title"];
    
    [review setObject:@"review_icon.png" forKey:@"icon"];
    [albums setObject:@"albums_icon.png" forKey:@"icon"];
    [myOrder setObject:@"order_icon.png" forKey:@"icon"];
    [myCoupon setObject:@"coupon_icon.png" forKey:@"icon"];
    [myCollect setObject:@"collect_icon.png" forKey:@"icon"];
    [activity setObject:@"activity_icon.png" forKey:@"icon"];
    
    [_menuList addObject:myOrder];
    [_menuList addObject:myCollect];
    [_menuList addObject:myCoupon];
    [_menuList addObject:activity];
    [_menuList addObject:review];
    [_menuList addObject:albums];
    
    self.menuTV.dataSource = _dataSource;
    self.menuTV.delegate = _dataSource;
    _dataSource.menuList = self.menuList;
}

- (void)rightButtonClick:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"您确定要退出登录吗？" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [AppSetting loginOut];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)backViewController
{
    if (_backController) {
        if (!_isHidden) {
            [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
        }
        
        [self.navigationController popToViewController:_backController animated:YES];
    }
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

@implementation LoggedDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
        _menuList = [NSMutableArray arrayWithCapacity:10];
        _userInfo = [NSMutableDictionary dictionaryWithCapacity:10];
    }
    return self;
}

- (void)fetchUserInfo
{
    NSString *url = [NSString stringWithFormat:API_INDEX,[AppSetting userName]];
    [self get:url tag:kINDEX];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    
    [self acceptData:data andRequestTag:request.tag];
}

- (void)acceptData:(NSDictionary *)data andRequestTag:(int)tag
{
    if (tag == kINDEX) {
        [_userInfo setObject:data forKey:@"data"];
        [AppSetting setIsLogin:YES];
        [AppSetting setTel:[data objectForKey:@"tel"]];
        [AppSetting setUserPhoto:[data objectForKey:@"touxiang"]];
        [AppSetting setNickName:[data objectForKey:@"nicheng"]];
        [_viewController.menuTV reloadData];
        [AppSetting setIsUpdatePhoto:NO];
        
        [_viewController performSelector:@selector(backViewController) withObject:nil afterDelay:1];
    }
    
    if (tag == kREVIEW) {
        ReviewViewController *reviewVC =
        [[ReviewViewController alloc] init];
        [self.viewController.navigationController pushViewController:reviewVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return 100;
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ([_menuList count] +1);
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *CellIdentifier = [self setCellIdentifier:indexPath.row];
    
    LoggedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"LoggedCell" owner:self options:nil];
        cell = [array objectAtIndex:[self setIndex:indexPath.row]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setUpUI:indexPath.row];
    if ([self judgeUserInfoData]) {
        [cell setUserInfo:[_userInfo objectForKey:@"data"]];
    }
    
    NSInteger row = indexPath.row-1;
    if ([self.menuList count] <= row) return cell;
    
    NSDictionary* dic = [self.menuList objectAtIndex:row];
    [cell setContect:dic];
    
    return cell;
}



#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self addSubView:indexPath.row];
    //详细信息页面
}

- (BOOL)judgeUserInfoData
{
    int tag = 0;
    for (id key in _userInfo) {
        tag++;
    }
    return tag > 0;
}

- (NSString*)setCellIdentifier:(int)tag
{
    if (tag != 0) return @"LoggedCell_1";
    
    return @"LoggedCell_0";
}

- (int)setIndex:(int)tag
{
    if (tag != 0)return 1;
    return 0;
}

- (void)addSubView:(int)tag
{
    switch (tag) {
        case 0: {
            UCenterViewController *memberVC =
            [[UCenterViewController alloc] init];
            [self.viewController.navigationController pushViewController:memberVC animated:YES];
            break;
        }
        case 1:{
            MyOrderViewController *myorderVC =
            [[MyOrderViewController alloc] init];
            [self.viewController.navigationController pushViewController:myorderVC animated:YES];
            break;
        }
        case 2:{
            MyCollectViewController *myCollectVC =
            [[MyCollectViewController alloc] init];
            [self.viewController.navigationController pushViewController:myCollectVC animated:YES];
            break;
        }
        case 3: {
            MyCouponViewController *myCouponVC =
            [[MyCouponViewController alloc] init];
            [self.viewController.navigationController pushViewController:myCouponVC animated:YES];
            break;
        }
        case 4: {
            ActivityViewController *activityVC =
            [[ActivityViewController alloc] init];
            [self.viewController.navigationController pushViewController:activityVC animated:YES];
            break;
        }
        case 5: {
            ReviewViewController *reviewVC =
            [[ReviewViewController alloc] init];
            [self.viewController.navigationController pushViewController:reviewVC animated:YES];
            break;
        }
        case 6: {
            MyPhotoViewController *photoView =
            [[MyPhotoViewController alloc] init];
            [self.viewController.navigationController pushViewController:photoView animated:YES];
            break;
        }
    }
}

@end

